//
//  ViewController.swift
//  SwiftRequest
//
//  Created by Pro on 2020/2/10.
//  Copyright © 2020 Pro. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift
import Moya
import Alamofire
import AlamofireNetworkActivityIndicator
import SnapKit

class ViewController: UIViewController {

    let disbag = DisposeBag()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "网络请求Demo"
        
        let button = UIButton.init(type: .system)
        self.view.addSubview(button)
        button.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(100)
            make.width.equalTo(100)
            make.height.equalTo(40)
        }
        button.setTitle("点击发起请求", for: .normal)
        button.backgroundColor = UIColor.cyan
        button.setTitleColor(UIColor.white, for: .normal)
        button.rx.controlEvent(.touchUpInside)
            .subscribe(onNext: { () in
                let request = TencentRequest(parameter: ["region":"北京","keyword":"美食","key":"YSJBZ-E7KWX-KJM4K-7Z6S7-TREBF-ILBJG"])
//                这里请求数据可以替换成自己的，key属于公司生产环境的资产。我并没有放在这里提供测试，请见谅YS
                    request.sendRequest { (Total) in
                        let total:RestanurantTotal = (Total as RestanurantTotal?)!
                        let resta:Restaurant = total.data.first! as! Restaurant
                        print("第一个地区的id是\(resta.id)")
                }
            })
        .disposed(by: disbag)
        
        
        let buttonNew = UIButton.init(type: .system)
        self.view.addSubview(buttonNew)
        buttonNew.snp.makeConstraints { (make) in
            make.left.equalTo(button.snp.right).offset(20)
            make.top.equalTo(button.snp.top)
            make.width.equalTo(button.snp.width)
            make.height.equalTo(button.snp.height)
        }
        buttonNew.setTitle("点击发起请求", for: .normal)
        buttonNew.backgroundColor = UIColor.yellow
        buttonNew.setTitleColor(UIColor.black, for: .normal)
        buttonNew.rx.controlEvent(.touchUpInside)
            .subscribe(onNext: { () in
                print("请求数据")
                let request = LHlhRequest(parameter: ["region":"北京","keyword":"美食","key":"YSJBZ-E7KWX-KJM4K-7Z6S7-TREBF-ILBJG"])
                LHAlamofireClient.default.send(request) {(Total) in
                           print(Total!)

                    let total:RestanurantTotal = (Total as RestanurantTotal?)!
                                    let resta:Restaurant = total.data.first! as! Restaurant
                                    print("第一个地区的id是\(resta.id)")
                }
         
                
                
                        
                
            })
        .disposed(by: disbag)
        
        
        let buttonThird = UIButton.init(type: .system)
        self.view.addSubview(buttonThird)
        buttonThird.snp.makeConstraints { (make) in
            make.left.equalTo(buttonNew.snp.right).offset(10)
            make.width.equalTo(buttonNew.snp.width)
            make.height.equalTo(buttonNew.snp.height)
            make.top.equalTo(buttonNew.snp.top)
        }
        
        buttonThird.backgroundColor = UIColor.purple
        buttonThird.setTitle("点击获取本地数据", for: .normal)
        buttonThird.setTitleColor(UIColor.white, for: .normal)
        buttonThird.rx.controlEvent(.touchUpInside)
            .subscribe(onNext: { () in
                                let request = LHlhRequest(parameter: ["region":"北京","keyword":"美食","key":"YSJBZ-E7KWX-KJM4K-7Z6S7-TREBF-ILBJG"])
                
                LHLocalClient().send(request) { (Total) in
//                        print(Total!)
                    let total:RestanurantTotal = (Total as RestanurantTotal?)!
                                    let resta:Restaurant = total.data.first! as! Restaurant
                                    print("第一个地区的id是\(resta.id)")
                    }

            })
        .disposed(by: disbag)
        
}
    
    
}

